package com.example.commoneventtest.slice;

import com.example.commoneventtest.MainAbility;
import com.example.commoneventtest.ResourceTable;
import com.example.commoneventtest.Utils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.event.commonevent.*;
import ohos.rpc.RemoteException;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private Button mBtnPublishNormalEvent;
    private Button mBtnPublishOrderedEvent;
    private Button mBtnPublishStickyEvent;
    private Button mBtnPublishSubscriptCustomEvent;
    private Button mBtnPublishUnsubscriptCustomEvent;

    // 公共事件订阅者
    private CommonEventSubscriber subscriber;

    // 自定义公共事件订阅者
    private CommonEventSubscriber customCommonEventSubscriber;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 通过MatchingSkills定义需要订阅的事件
        MatchingSkills matchingSkills = new MatchingSkills();
        // 订阅开始充电公共事件
        matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_CHARGING);
        // 通过CommonEventSubscribeInfo对象设置订阅参数
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        // 实例化订阅者
        subscriber = new CommonEventSubscriber(subscribeInfo) {
            @Override
            public void onReceiveEvent(CommonEventData commonEventData) {
                Utils.showToast(MainAbilitySlice.this, "开始充电!");
            }
        };

        // 通过CommonEventManager开始订阅
        try {
            CommonEventManager.subscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            Utils.log("订阅失败:" + e.getLocalizedMessage());
        }

        MatchingSkills skills = new MatchingSkills();
        // 订阅普通公共事件
        skills.addEvent("com.example.commoneventtest.NormalCommonEvent");
        // 订阅有序公共事件
        skills.addEvent("com.example.commoneventtest.OrderedCommonEvent");
        // 订阅粘性公共事件
        skills.addEvent("com.example.commoneventtest.StickyCommonEvent");
        // 实例化自定义公共事件订阅者
        customCommonEventSubscriber = new CommonEventSubscriber(
                new CommonEventSubscribeInfo(skills)) {
            @Override
            public void onReceiveEvent(CommonEventData data) {
                // 公共事件字符串
                String action = data.getIntent().getAction();
                if (action == "com.example.commoneventtest.NormalCommonEvent") {
                    Utils.showToast(MainAbilitySlice.this, "接收普通公共事件");
                }
                if (action == "com.example.commoneventtest.OrderedCommonEvent") {
                    Utils.showToast(MainAbilitySlice.this,
                            "接收有序公共事件.结果码: " + data.getCode()
                                    + " 结果数据: " + data.getData());
                }
                if (action == "com.example.commoneventtest.StickyCommonEvent") {
                    Utils.showToast(MainAbilitySlice.this, "接收粘性公共事件");
                }
            }
        };


        mBtnPublishNormalEvent = (Button) findComponentById(ResourceTable.Id_btn_publish_normal_event);
        mBtnPublishNormalEvent.setClickedListener(this);
        mBtnPublishOrderedEvent = (Button) findComponentById(ResourceTable.Id_btn_publish_ordered_event);
        mBtnPublishOrderedEvent.setClickedListener(this);
        mBtnPublishStickyEvent = (Button) findComponentById(ResourceTable.Id_btn_publish_sticky_event);
        mBtnPublishStickyEvent.setClickedListener(this);
        mBtnPublishSubscriptCustomEvent = (Button) findComponentById(ResourceTable.Id_btn_publish_subscript_custom_event);
        mBtnPublishSubscriptCustomEvent.setClickedListener(this);
        mBtnPublishUnsubscriptCustomEvent = (Button) findComponentById(ResourceTable.Id_btn_publish_unsubscript_custom_event);
        mBtnPublishUnsubscriptCustomEvent.setClickedListener(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        try {
            CommonEventManager.unsubscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            Utils.log("退订失败:" + e.getLocalizedMessage());
        }
    }

    @Override
    public void onClick(Component component) {
        // 发布普通公共事件
        if (component.getId() == ResourceTable.Id_btn_publish_normal_event) {
            try {
                // 公共事件Intent对象，定义了公共事件字符串
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("com.example.commoneventtest.NormalCommonEvent")
                        .build();
                intent.setOperation(operation);
                // 创建CommonEventData对象
                CommonEventData eventData = new CommonEventData(intent);
                // 发布公共事件
                CommonEventManager.publishCommonEvent(eventData);
            } catch (RemoteException e) {
                Utils.log("发布普通公共事件失败:" + e.getLocalizedMessage());
            }
        }
        // 发布有序公共事件
        if (component.getId() == ResourceTable.Id_btn_publish_ordered_event) {
            try {
                // 公共事件Intent对象，定义了公共事件字符串
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("com.example.commoneventtest.OrderedCommonEvent")
                        .build();
                intent.setOperation(operation);
                // 创建CommonEventData对象
                CommonEventData eventData = new CommonEventData(intent);
                eventData.setCode(0); // 设置结果码
                eventData.setData("初始数据"); // 设置结果数据
                // 创建CommonEventPublishInfo对象
                CommonEventPublishInfo info = new CommonEventPublishInfo();
                info.setOrdered(true); // 定义有序公共事件
                // 发布公共事件
                CommonEventManager.publishCommonEvent(eventData, info);
            } catch (RemoteException e) {
                Utils.log("发布有序公共事件失败:" + e.getLocalizedMessage());
            }
        }
        // 发布粘性公共事件
        if (component.getId() == ResourceTable.Id_btn_publish_sticky_event) {
            try {
                // 公共事件Intent对象，定义了公共事件字符串
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withAction("com.example.commoneventtest.StickyCommonEvent")
                        .build();
                intent.setOperation(operation);
                // 创建CommonEventData对象
                CommonEventData eventData = new CommonEventData(intent);
                // 创建CommonEventPublishInfo对象
                CommonEventPublishInfo info = new CommonEventPublishInfo();
                info.setSticky(true); // 定义粘性公共事件
                // 发布公共事件
                CommonEventManager.publishCommonEvent(eventData, info);
            } catch (RemoteException e) {
                Utils.log("发布粘性公共事件失败:" + e.getLocalizedMessage());
            }
        }
        // 订阅自定义公共事件
        if (component.getId() == ResourceTable.Id_btn_publish_subscript_custom_event) {
            try {
                CommonEventManager.subscribeCommonEvent(customCommonEventSubscriber);
            } catch (RemoteException e) {
                Utils.log("订阅失败:" + e.getLocalizedMessage());
            }

        }
        // 取消订阅自定义公共事件
        if (component.getId() == ResourceTable.Id_btn_publish_unsubscript_custom_event) {
            try {
                CommonEventManager.unsubscribeCommonEvent(customCommonEventSubscriber);
            } catch (RemoteException e) {
                Utils.log("退订失败:" + e.getLocalizedMessage());
            }
        }
    }
}
